﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Common;

namespace MM.DBUtility
{
    public class BLSqlDataSPIO
    {
        #region SP 接收数据
        public static string GetEncoding(string strValue)
        {
            string strResult = string.Empty;
            if (!string.IsNullOrEmpty(strValue))
            {
                byte[] bytes = System.Text.Encoding.Default.GetBytes(strValue);
                strResult = System.Text.Encoding.UTF8.GetString(bytes);
            }
            return strResult;

        }
        public static void ReceiveDataV2Mini(string strsmstype, string strlinkid, string strmobile, string strcontent,
            string strspnum, string strfeecode, string strprov, string stricp, string strstatus, string spcode)
        {
            DbHelperSql.ExecuteNonQuerySP(BLSqlDataChargeV2.ConnStrV2, "SP_SPIOReceive",
                       new SqlParameter("@linkid", strlinkid.Trim()),
                       new SqlParameter("@mobile", strmobile.Trim()),
                       new SqlParameter("@content", strcontent.Trim()),
                       new SqlParameter("@spnum", strspnum.Trim()),
                       new SqlParameter("@status", strstatus.Trim()),
                       new SqlParameter("@feecode", strfeecode.Trim()),
                       new SqlParameter("@province", strprov.Trim()),
                       new SqlParameter("@spid", spcode.Trim()),
                       new SqlParameter("@icp", stricp.Trim()));
        }
        public static void ReceiveDataV2Mini(string strlinkid, string strmobile, string strcontent,
    string strspnum, string strstatus, string spcode)
        {
            DbHelperSql.ExecuteNonQuerySP(BLSqlDataChargeV2.ConnStrV2, "SP_SPIOReceive",
                       new SqlParameter("@linkid", strlinkid.Trim()),
                       new SqlParameter("@mobile", strmobile.Trim()),
                       new SqlParameter("@content", strcontent.Trim()),
                       new SqlParameter("@spnum", strspnum.Trim()),
                       new SqlParameter("@spid", spcode.Trim()),
                       new SqlParameter("@status", strstatus.Trim()));
        }
        public static void ReceiveDataV2(string strlinkid, string strmobile, string strcontent,
            string strspnum, string strstatus)
        {
            DbHelperSql.ExecuteNonQuerySP(BLSqlDataChargeV2.ConnStrV2, "SP_SPIOReceive",
                       new SqlParameter("@linkid", strlinkid.Trim()),
                       new SqlParameter("@mobile", strmobile.Trim()),
                       new SqlParameter("@content", strcontent.Trim()),
                       new SqlParameter("@spnum", strspnum.Trim()),
                       new SqlParameter("@status", strstatus.Trim()));
        }
        public static void ReceiveData1V2(string strlinkid, string strmobile, string strcontent,
    string strspnum, string strstatus)
        {
            DbHelperSql.ExecuteNonQuerySP(BLSqlDataChargeV2.ConnStrV2, "SP_SPIOReceive1",
                       new SqlParameter("@linkid", strlinkid.Trim()),
                       new SqlParameter("@mobile", strmobile.Trim()),
                       new SqlParameter("@content", strcontent.Trim()),
                       new SqlParameter("@spnum", strspnum.Trim()),
                       new SqlParameter("@status", strstatus.Trim()));
        }
        /// <summary>
        /// 华金楠，深圳信驰，深圳鼎鑫，深圳六吉，深圳瑞通，网鑫科技数据接收
        /// </summary>
        /// <param name="strlinkid">唯一标识</param>
        /// <param name="strmobile">手机号</param>
        /// <param name="strcontent">内容</param>
        /// <param name="strspnum">通道号码</param>
        /// <param name="strstatus">状态报告DELIVRD表示成功</param>
        public static void ReceiveDatahjn(string strlinkid, string strmobile, string strcontent,
            string strspnum, string strstatus)
        {
            ReceiveDataV2(strlinkid, strmobile, strcontent, strspnum, strstatus);
        }
        /// <summary>
        /// 深圳市秦汉达科技开发有限公司
        /// </summary>
        /// <param name="strlinkid">唯一标识</param>
        /// <param name="strmobile">手机号</param>
        /// <param name="strcontent">内容</param>
        /// <param name="strspnum">通道号码</param>
        /// <param name="strstatus">状态报告DELIVRD表示成功</param>
        public static void ReceiveDataqhd(string strlinkid, string strmobile, string strcontent,
            string strspnum, string strstatus)
        {
            ReceiveDataV2(strlinkid, strmobile, strcontent, strspnum, strstatus);
        }
        /// <summary>
        /// 运程无限数据接收
        /// </summary>
        /// <param name="strlinkid">唯一标识</param>
        /// <param name="strmobile">手机号</param>
        /// <param name="strcontent">内容</param>
        /// <param name="strspnum">通道号码</param>
        /// <param name="strstatus">状态报告DELIVRD表示成功</param>
        public static void ReceiveDataycwx(string strlinkid, string strmobile, string strcontent,
            string strspnum, string strstatus)
        {
            ReceiveDataV2(strlinkid, strmobile, strcontent, strspnum, strstatus);
        }
        /// <summary>
        /// 天津炎黄数据接收
        /// </summary>
        /// <param name="strsmstype">类型</param>
        /// <param name="strlinkid">唯一标识</param>
        /// <param name="strmobile">手机号</param>
        /// <param name="strcontent">内容</param>
        /// <param name="strspnum">通道号码</param>
        /// <param name="strfeecode">资费 100 表示1.00元</param>
        /// <param name="strprov">省份</param>
        /// <param name="stricp"></param>
        /// <param name="strstatus">状态报告DELIVRD表示成功</param>
        public static void ReceiveData(string strsmstype, string strlinkid, string strmobile, string strcontent,
            string strspnum, string strfeecode, string strprov, string stricp, string strstatus)
        {
            DbHelperSql.ExecuteNonQuerySP(BLSqlDataChargeV2.ConnStrV2, "SP_SPIOReceive",
                       new SqlParameter("@linkid", strlinkid.Trim()),
                       new SqlParameter("@mobile", strmobile.Trim()),
                       new SqlParameter("@content", strcontent.Trim()),
                       new SqlParameter("@spnum", strspnum.Trim()),
                       new SqlParameter("@status", strstatus.Trim()),
                       new SqlParameter("@feecode", strfeecode.Trim()),
                       new SqlParameter("@province", strprov.Trim()),
                       new SqlParameter("@icp", stricp.Trim()));
        }
        /// <summary>
        /// 掌上灵通数据接收
        /// </summary>
        /// <param name="strXML"></param>
        public static void ReceiveDatazslt(string strXML)
        {
            XmlHelper Xhelp = new XmlHelper(strXML);
            string strlinkid = Xhelp.ReadXMLNodeValue("linkid");

            if (!string.IsNullOrEmpty(strlinkid))
                DbHelperSql.ExecuteNonQuerySP(BLSqlDataChargeV2.ConnStrV2, "SP_SPIOReceive",
                           new SqlParameter("@linkid", strlinkid.Trim()),
                           new SqlParameter("@mobile", Xhelp.ReadXMLNodeValue("mobile").Trim()),
                           new SqlParameter("@content", Xhelp.ReadXMLNodeValue("content").Trim()),
                           new SqlParameter("@spnum", Xhelp.ReadXMLNodeValue("spcode").Trim()),
                           new SqlParameter("@status", Xhelp.ReadXMLNodeValue("statestr").Trim()),
                           new SqlParameter("@feecode", Xhelp.ReadXMLNodeValue("feeprice").Trim()),
                           new SqlParameter("@feeid", Xhelp.ReadXMLNodeValue("feecode").Trim()),
                           new SqlParameter("@feetype", Xhelp.ReadXMLNodeValue("feetype").Trim()),
                           new SqlParameter("@feecategory", Xhelp.ReadXMLNodeValue("feecategory").Trim()),
                           new SqlParameter("@product", Xhelp.ReadXMLNodeValue("product").Trim()),
                           new SqlParameter("@channel", Xhelp.ReadXMLNodeValue("channel").Trim()),
                           new SqlParameter("@isprovision", Xhelp.ReadXMLNodeValue("isprovision").Trim()),
                           new SqlParameter("@issubscribe", Xhelp.ReadXMLNodeValue("issubscribe").Trim()),
                           new SqlParameter("@icp", Xhelp.ReadXMLNodeValue("toicp").Trim()));
        }
        /// <summary>
        /// 深圳尖峰数据接收
        /// </summary>
        /// <param name="strXML"></param>
        public static void ReceiveDataszjf(string strXML)
        {
            string[] artTemp = strXML.Split('&');
            string strlinkid = artTemp.ToList().Find(p => p.Contains("linkid=")).Replace("linkid=", "");
            if (!string.IsNullOrEmpty(strlinkid))
                DbHelperSql.ExecuteNonQuerySP(BLSqlDataChargeV2.ConnStrV2, "SP_SPIOReceive",
                           new SqlParameter("@linkid", strlinkid.Trim()),
                           new SqlParameter("@mobile", artTemp.ToList().Find(p => p.Contains("mobilenum=")).Replace("mobilenum=", "").Trim()),
                           new SqlParameter("@content", artTemp.ToList().Find(p => p.Contains("msg=")).Replace("msg=", "").Trim()),
                           new SqlParameter("@spnum", artTemp.ToList().Find(p => p.Contains("spnumber=")).Replace("spnumber=", "").Trim()));
        }
        #endregion

        #region IVR 接收数据
        public static void IVRReceiveData(string linkid, string spnum, string mobile,
    string begintime, string ivrtime, string feecode)
        {
            DbHelperSql.ExecuteNonQuerySP(BLSqlDataChargeV2.ConnStrV2, "SP_IVRIOReceive",
                       new SqlParameter("@linkid", linkid.Trim()),
                       new SqlParameter("@spnum", spnum.Trim()),
                       new SqlParameter("@mobile", mobile.Trim()),
                       new SqlParameter("@begintime", begintime.Trim()),
                       new SqlParameter("@ivrtime", ivrtime.Trim()),
                       new SqlParameter("@feecode", feecode.Trim()));
        }
        public static void IVRReceiveData(string linkid, string spnum, string mobile,
    string begintime, string ivrtime, string feecode, string spcode)
        {
            DbHelperSql.ExecuteNonQuerySP(BLSqlDataChargeV2.ConnStrV2, "SP_IVRIOReceive",
                       new SqlParameter("@linkid", linkid.Trim()),
                       new SqlParameter("@spcode", spcode.Trim()),
                       new SqlParameter("@spnum", spnum.Trim()),
                       new SqlParameter("@mobile", mobile.Trim()),
                       new SqlParameter("@begintime", begintime.Trim()),
                       new SqlParameter("@ivrtime", ivrtime.Trim()),
                       new SqlParameter("@feecode", feecode.Trim()));
        }
        #endregion


        #region 其它方法
        public static void OpenUrlSendMR(string strUrl)
        {
            try
            {
                //System.Net.WebClient wc = new System.Net.WebClient();
                //wc.OpenReadAsync(new Uri(strUrl));

                System.Net.HttpWebRequest myReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);
                myReq.Method = "GET";
                myReq.ContentType = "text/xml";
                myReq.GetResponse();
            }
            catch (Exception ex)
            {
                MM.Common.LogHelp.WriteLogInfo(ex.ToString());
            }
        }
        #endregion
    }
}
